﻿// =================================================================== 
// 数据访问层(GDIT.DAL)
//====================================================================
// 文件：Mtb_MainMenus.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：栏目总体分类数据访问层(导航栏菜单)
// ===================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Model;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using GDIT.Pattern;

namespace GDIT.DAL
{
    public class DMainMenus : IMainMenus
    {

        private XpoHelper.Test<Mtb_MainMenus> userOperation = null;

        /// <summary>
        /// 新增主菜单
        /// </summary>
        /// <param name="detailMenu">需要新增的主菜单对象</param>
        /// <returns>1：新增成功 2：新增失败</returns>
        public int InsertMainMenus(Mtb_MainMenus mainMenus)
        {
            return XpoHelper.SaveAdded(mainMenus);
        }

        /// <summary>
        /// 删除主菜单
        /// </summary>
        /// <param name="detailMenu">需要删除的主菜单对象</param>
        /// <returns>1：删除成功 0：删除失败</returns>
        public int DeleteMainMenus(Mtb_MainMenus mainMenus)
        {
            userOperation = new XpoHelper.Test<Mtb_MainMenus>();
            return userOperation.Delete(mainMenus.OID);
        }

        /// <summary>
        /// 更新主菜单
        /// </summary>
        /// <param name="detailMenu">需要更新的主菜单对象</param>
        /// <returns>1：更新成功 0：更新失败</returns>
        public int UpdateMainMenus(Mtb_MainMenus mainMenus)
        {
            userOperation = new XpoHelper.Test<Mtb_MainMenus>();
            XPCollection xp = userOperation.Operation("OID", mainMenus.OID);
            foreach (Mtb_MainMenus b in xp)
            {
                b.MenuName = mainMenus.MenuName;
                b.LinkAddress = mainMenus.LinkAddress;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 查询所有的主菜单列表
        /// </summary>
        /// <returns>所有的主菜单列表</returns>
        public XPCollection SelectMainMenus()
        {
            userOperation = new XpoHelper.Test<Mtb_MainMenus>();
            return userOperation.Operation();
        }

        /// <summary>
        /// 查询单一的主菜单对象
        /// </summary>
        /// <param name="detailMenu">需要查询的主菜单对象</param>
        /// <returns>单一的主菜单对象</returns>
        public Mtb_MainMenus SelectMainMenusByMain(Mtb_MainMenus mainMenus)
        {
            Mtb_MainMenus bulletin = null;
            userOperation = new XpoHelper.Test<Mtb_MainMenus>();
            XPCollection xp = userOperation.Operation("OID", mainMenus.OID);
            foreach (Mtb_MainMenus b in xp)
            {
                bulletin = b;
            }
            return bulletin;
        }

        /// <summary>
        /// 新增详细菜单
        /// </summary>
        /// <param name="mainMenus"></param>
        /// <returns></returns>
        public int InsertDetailMenu(Mtb_MainMenus mainMenus)
        {
            int result = 0;
            XPCollection xp = new XPCollection(typeof(Mtb_MainMenus),new BinaryOperator("OID",mainMenus.OID));
            foreach (Mtb_MainMenus b in xp)
            {
                b.Details.AddRange(mainMenus.Details);
                result = XpoHelper.SaveModifiedByZi(b);
            }
            return result;
        }
    }
}
